#include "../include/stdio.h"
#include "../include/console.h"
#include "../include/types.h"
#include "../include/uart.h"

// 基础printf测试
void test_printf_basic() {
    printf(COLOR_CYAN "=== 1. Basic printf Test ===\n" COLOR_RESET);
    printf("Decimal positive: %d\n", 42);
    printf("Decimal negative: %d\n", -123);
    printf("Decimal zero: %d\n", 0);
    printf("Hexadecimal (0xABC): 0x%x\n", 0xABC);
    printf("String: %s\n", "Hello RISC-V OS");
    printf("Character: %c\n", 'X');
    printf("Percent sign: %%\n");
}

//边缘数值测试
void test_printf_edge_cases() {
printf(COLOR_CYAN "=== 2. Edge cases Test ===\n" COLOR_RESET);
printf("INT_MAX: %d\n", INT_MAX);
printf("INT_MIN: %d\n", INT_MIN);
printf("NULL string: %s\n", (char*)0);
printf("Empty string: %s\n", "");
}

/*//sprintf 测试（格式化输出到缓冲区）
void test_sprintf() {
    printf(COLOR_CYAN "=== 3. sprintf Test ===\n" COLOR_RESET);
    char buf[128];
    // 格式化内容到缓冲区
    int len = sprintf(buf, "sprintf result: num=%d, hex=0x%x, str=%s", 100, 0xFF, "formatted");
    // 输出缓冲区内容和长度
    printf("Buffer content: %s\n", buf);
    printf("Buffer length: %d bytes\n\n", len);
}
*/

// 颜色输出测试（验证 8 种基础颜色）
void test_color_output() {
    printf(COLOR_CYAN "=== 3. Color Output Test ===\n" COLOR_RESET);
    printf_color(COLOR_RED, "Red text: This is red\n");
    printf_color(COLOR_GREEN, "Green text: This is green\n");
    printf_color(COLOR_YELLOW, "Yellow text: This is yellow\n");
    printf_color(COLOR_BLUE, "Blue text: This is blue\n");
    printf_color(COLOR_MAGENTA, "Magenta text: This is magenta\n");
    printf_color(COLOR_CYAN, "Cyan text: This is cyan\n");
    printf_color(COLOR_WHITE, "White text: This is white\n");
    printf_color(COLOR_BLACK, "Black text: This is black (may be invisible on dark background)\n");
    printf(COLOR_RESET "Back to default color\n\n");  // 重置为默认颜色
}

// 控制台控制测试（清屏、光标定位、删除行）
void test_console_control() {
    printf(COLOR_CYAN "=== 4. Console Control Test ===\n" COLOR_RESET);
    printf("Will clear screen in 3 seconds... (countdown: 3) ");
    for (int i = 0; i < 100000000; i++);  // 延时1秒
    printf("\b2 ");  // 回退修改倒计时
    for (int i = 0; i < 100000000; i++);
    printf("\b1 ");
    for (int i = 0; i < 100000000; i++);
    printf("\b0\n");
    
    clear_screen();  // 清屏
    printf(COLOR_GREEN "Screen cleared!\n" COLOR_RESET);
    
    // 光标定位测试（x=列，y=行，从1开始）
    goto_xy(5, 3);    // 移动光标到第3行第5列
    printf("This text is at (x=5, y=3)");
    
    goto_xy(10, 6);   // 移动光标到第6行第10列
    printf("This line will be cleared in 2 seconds...");
    for (int i = 0; i < 500000000; i++);  // 延时5秒
    
    goto_xy(10, 6);   // 回到第6行第10列
    clear_line();     // 删除当前行（从光标位置到行尾）
    printf(COLOR_YELLOW "Line cleared!\n" COLOR_RESET);
    
    goto_xy(1, 10);   // 移动光标到第10行第1列
    printf(COLOR_GREEN "All console control tests completed!\n" COLOR_RESET);
}

// 执行所有测试
void kernel_main() {
    console_init();  // 初始化控制台（含UART初始化）
    // 依次执行所有测试
    test_printf_basic();
    test_printf_edge_cases();
    //test_sprintf();
    test_color_output();
    test_console_control();
    
    // 死循环，防止内核退出
    while (1);
}
